package com.tensquare.friend.controller;

import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    FriendService friendService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private UserClient userClient;

    /**
     *
     * 添加好友或者添加非好友
     * @return
     */

    @RequestMapping(value = "/like/{friendid}/{type}",method = RequestMethod.PUT)
    public Result addFriend(@PathVariable String friendid,@PathVariable String type){
        //验证是否登录，并且拿到当前登录的用户id
        Claims claims = (Claims) request.getAttribute("claims_user");
        if (null == claims || "".equals(claims)){
            return new Result(false, StatusCode.LOGINERROR,"请先登录");
        }
        //得到当前的用户id
        String userid = claims.getId();
        if (null == userid){
            return new Result(false, StatusCode.LOGINERROR,"请先登录");
        }
        System.out.println("FriendController userid:"+userid);
        //判断一下，是添加好友还是添加非好友
        if (null != type){
            if ("1".equals(type)){
                //添加好友
                int flag = friendService.addFriend(userid,friendid);
                if (0 == flag){
                    return new Result(false,StatusCode.ERROR,"不能重复添加");
                }
                if (1 == flag){
                    //我们在添加成功之前向user模块进行调用
                    userClient.updateFansCountAndFollowCount(userid,friendid,1);
                    return new Result(true,StatusCode.OK,"添加成功");
                }
            }else if ("2".equals(type)){
                //添加非好友
                int flag = friendService.addNofriend(userid,friendid);
                if (0 == flag){
                    return new Result(false,StatusCode.ERROR,"不能重复添加");
                }
                if (1 == flag){
                    //
                    return new Result(true,StatusCode.OK,"添加成功");
                }
            }
            return new Result(false,StatusCode.ERROR,"参数异常");
        }else{
            return new Result(false,StatusCode.ERROR,"参数异常");
        }
    }


    //删除好友
    @RequestMapping(value = "/{friendid}",method = RequestMethod.DELETE)
    public Result deleteFriend(@PathVariable String friendid){
        //验证是否登录，并拿到当前登录的用户id
        Claims claims = (Claims) request.getAttribute("claims_user");
        if (null == claims || "".equals(claims)){
            return new Result(false,StatusCode.LOGINERROR,"请先登录");
        }
        //获得当前用户id
        String userid = claims.getId();
        if (null == userid){
            return new Result(false, StatusCode.LOGINERROR,"请先登录");
        }
        System.out.println("FriendController userid:"+userid);
        friendService.deleteFriend(userid,friendid);
        //调用user服务更改数据
        userClient.updateFansCountAndFollowCount(userid,friendid,-1);
        return new Result(true,StatusCode.OK,"删除成功");
    }






}
